package com.zzyl.serve.service.impl;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.serve.vo.TimeCountVo;
import org.apache.poi.xwpf.usermodel.LineSpacingRule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.ReservationMapper;
import com.zzyl.serve.domain.Reservation;
import com.zzyl.serve.service.IReservationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;

/**
 * 预约信息Service业务层处理
 * 
 * @author koi
 * @date 2025-05-09
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService
{
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 查询预约信息
     * 
     * @param id 预约信息主键
     * @return 预约信息
     */
    @Override
    public Reservation selectReservationById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询预约信息列表
     * 
     * @param reservation 预约信息
     * @return 预约信息
     */
    @Override
    public List<Reservation> selectReservationList(Reservation reservation)
    {
        return reservationMapper.selectReservationList(reservation);
    }

    /**
     * 新增预约信息
     * 
     * @param reservation1 预约信息
     * @return 结果
     */
    @Override
    public int insertReservation(Reservation reservation1)
    {
        Long  userId = UserThreadLocal.getUserId();
        //Reservation reservation = BeanUtil.toBean(reservationDto, Reservation.class);
        Reservation reservation =BeanUtil.toBean(reservation1, Reservation.class);
        reservation.setCreateBy(userId+"");
        reservation.setUpdateBy(userId+"");
        reservation.setStatus(0);
        return save(reservation) ? 1 : 0;
    }

    /**
     * 修改预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int updateReservation(Reservation reservation)
    {
        return updateById(reservation) ? 1 : 0;
    }

    /**
     * 批量删除预约信息
     * 
     * @param ids 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除预约信息信息
     * 
     * @param id 预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 分页查询预约信息
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */

    @Override
    public TableDataInfo ReservationPage(int pageNum, int pageSize, Integer status) {
        Long userId = UserThreadLocal.getUserId();
        if (ObjectUtils.isEmpty(userId)){
           throw  new BaseException("用户未登录");
        }
        LambdaQueryWrapper<Reservation> queryWrapper = new LambdaQueryWrapper<Reservation>()
                .eq (Reservation::getCreateBy,userId)
                .eq(ObjectUtil.isNotEmpty(status),Reservation::getStatus,status);
                 queryWrapper.orderByDesc(Reservation::getCreateTime);

        Page page = new Page<>(pageNum,pageSize);
//        Wrapper<Reservation> LambdaQueryWrapper = null;
        page(page,queryWrapper);

        return createTableDataInfo(page);
    }

    private TableDataInfo createTableDataInfo(Page page) {

      TableDataInfo tableDataInfo = new TableDataInfo();
      tableDataInfo.setTotal(page.getTotal());
      tableDataInfo.setRows(page.getRecords());
      tableDataInfo.setCode(200);
      tableDataInfo.setMsg("查询成功");
      return tableDataInfo;
    }


    /**
     * 取消预约
     * @param id
     * @return
     */

    @Override
    public int cancelReservation(Long id) {
        Reservation reservation = getById(id);
        if(ObjectUtil.isEmpty(reservation)){
            throw new BaseException("预约不存在");
        }
        reservation.setStatus(2);
        reservation.setUpdateBy(UserThreadLocal.getUserId()+"");
        return updateById(reservation) ? 1 : 0;
    }

    /**
     * 2.1 查询当天取消预约数量
     * @param userId
     * @return
     */

//    @Override
//    public int getCancelledReservationCount(Long userId) {
//        long time = System.currentTimeMillis();
//        LocalDateTime ldt = LocalDateTimeUtil.of(time);
//        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
//        LocalDateTime endTime = startTime.plusDays(1);
//        LambdaQueryWrapper<Reservation> queryWrapper = new LambdaQueryWrapper<Reservation>()
//                .eq(Reservation::getUpdateBy,userId)
//                .between(Reservation::getUpdateTime,startTime,endTime);
//         return (int) count(lambdaQuery().getWrapper());
////        return reservationMapper .cou(getCancelledReservationCount(userId));
//    }

    @Override
    public int getCancelledCoun(Long userId) {
        LocalDateTime startTime = LocalDate.now().atStartOfDay();
        LocalDateTime endTime = startTime.plusHours(24);
        LambdaQueryWrapper<Reservation> queryWrapper = new LambdaQueryWrapper<Reservation>()
                .eq(Reservation::getCreateBy,userId)
                .between(Reservation::getCreateTime,startTime,endTime);
        return (int) count(queryWrapper);
    }

    @Override
    public List<TimeCountVo> countReservationsForTim(Long time) {
        LocalDateTime localDateTime = LocalDateTimeUtil.of(time);

        LocalDateTime statrtTime = localDateTime.toLocalDate().atStartOfDay();

        LocalDateTime endTime = statrtTime.plusHours(24);
        List<TimeCountVo> timeCountVoList = reservationMapper.countReservationsForTim(statrtTime, endTime);
        return timeCountVoList;
    }
}
